﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication.Controls
{
    public partial class MainHUD1 : UserControl
    {
        protected bool _hasFocus;
        public bool HasFocus
        {
            get { return _hasFocus; }
            set { SetFocus(); }
        }

        public static readonly DependencyProperty EnabledProperty = DependencyProperty.Register(
            "Enabled", typeof(bool), typeof(MainHUD1), new PropertyMetadata(false, new PropertyChangedCallback(EnabledChanged)));

        public bool Enabled
        {
            get { return (bool)GetValue(EnabledProperty); }
            set { SetValue(EnabledProperty, value); }
        }

        private static void EnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MainHUD1 t = (MainHUD1)d;
            t.Enabled = (bool)e.NewValue;
            t.UpdateBindings();
        }

        public void UpdateBindings()
        {
            if (Enabled)
            {
                App.MainDispatcher.BeginInvoke(() => { this.Visibility = Visibility.Visible; txtInput.Focus(); });
            }
            else
            {
                App.MainDispatcher.BeginInvoke(() => this.Visibility = Visibility.Collapsed);
            }
        }

        public MainHUD1()
        {
            InitializeComponent();
            topHUDControl.WantShowCharacter += new EventHandler(topHUDControl_WantShowCharacter);
            topHUDControl.WantFindGameThings += new EventHandler(topHUDControl_WantFindGameThings);
            LostFocus += new RoutedEventHandler(MainHUD1_LostFocus);
        }

        void MainHUD1_LostFocus(object sender, RoutedEventArgs e)
        {
            _hasFocus = false;
        }

        private void edit_RequestEditID(object sender, ObjectEventArgsNonRef<long> e)
        {
            OnEditId(e.obj);
        }

        public void OnEditId(long id)
        {
            wc2.Visibility = Visibility.Visible;
            IGameThing thing = Everything.Find<IGameThing>(id);
            editTemplate.EditObj = thing;
        }

        void topHUDControl_WantFindGameThings(object sender, EventArgs e)
        {
            findThings.Toggle();
        }

        void topHUDControl_WantShowCharacter(object sender, EventArgs e)
        {
            if (showCharDetails.Toggle())
            {
                showCharDetails.GameChar = DataAccess.CurrentlyPlaying;
            }
        }

        private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;

                string input = txtInput.Text;
                notificationsControl.AppendNotificationColor(input, Color.FromArgb(255, 0, 100, 0), Color.FromArgb(255, 255, 255, 0));
                txtInput.Text = string.Empty;
                DataAccess.Client.SendCommandAsync(DataAccess.AssignedUniqueId, input);
            }
            else if (e.Key == Key.A && map.MapCanvas.WorldMap == null)
            {
                map.MapCanvas.WorldMap = DataAccess.LocalWorld;
            }
            map.HandleKeydown(sender, e);
        }

        public void AppendNotificationText(string s)
        {
            notificationsControl.AppendNotification(s);
        }

        public void SetFocus()
        {
            txtInput.Focus();
            _hasFocus = true;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            txtInput.Focus();
            DataVisualizerRegistry.Instance.HeightChanged += new EventHandler<ObjectEventArgsNonRef<double>>(Instance_HeightChanged);
        }

        void Instance_HeightChanged(object sender, ObjectEventArgsNonRef<double> e)
        {
            double height = e.obj;
            double newHeight = height - 60;

            if (newHeight > notificationsControl.MinHeight)
                notificationsControl.Height = newHeight;
        }

        private void ShowCharacterDetails1_WantClose(object sender, EventArgs e)
        {
            showCharDetails.Toggle();
        }

        private void findThings_RequestEditID(object sender, ObjectEventArgsNonRef<long> e)
        {
            OnEditId(e.obj);
        }

        private void WindowContainer_CloseWindow(object sender, EventArgs e)
        {
            wc2.Visibility = Visibility.Collapsed;
        }

        private void findThings_CloseWindow(object sender, EventArgs e)
        {
            findThings.Toggle();
        }

        private void findThings_RequestGoLocation(object sender, ObjectEventArgs<LocationInt> e)
        {
            string command = "GOTO " + e.obj.ToString();
            DataAccess.Client.SendCommandAsync(DataAccess.AssignedUniqueId, command);
        }

        public void UpdateLocation(long id, LocationInt loc, long location)
        {
            topHUDControl.UpdateLocation(id, loc, location);
        }

        public void OnRequestEditGameLocation(LocationInt e)
        {
            GameLocation loc = DataAccess.LocalWorld.FindItem(e.X, e.Y, e.Z);
            if (loc != null)
            {
                OnEditId(loc.AssociatedID);
            }
        }

        private void findThings_RequestEditGameLocation(object sender, ObjectEventArgs<LocationInt> e)
        {
            OnRequestEditGameLocation(e.obj);
        }

        private void topHUDControl_RequestEditGameLocation(object sender, ObjectEventArgs<LocationInt> e)
        {
            OnRequestEditGameLocation(e.obj);
        }
    }
}
